package com.linh.location;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LocationParser {

	public LocationParser() {

	}

	public Map<String, BusStop> parse(String jasons) {
		Map<String, BusStop> busMap = new HashMap<String, BusStop>();
		try {
			Scanner scanner = new Scanner(jasons);
			if (scanner.hasNext()) {
				scanner.next();
			}
			for (; scanner.hasNext();) {
				try {
					String line = scanner.nextLine();

					String[] attrs = line.replace("[", "").replace("]", "")
							.replace("\"", "").split(",");
					if (attrs.length == 9) {
						String busStopId = attrs[2];
						BusStop busStop = busMap.get(busStopId);
						if (busStop == null) {
							busStop = new BusStop();
							busStop.setStopId(busStopId);
							busStop.setName(attrs[1]);
							busStop.setLatitude(Double.parseDouble(attrs[4]));
							busStop.setLongitude(Double.parseDouble(attrs[5]));
							busMap.put(busStopId, busStop);

						}

						// parse route
						long estimatedTime = new BigInteger(attrs[8])
								.longValue();
						BusRoute busRoute = new BusRoute(attrs[3], attrs[7],
								attrs[6], estimatedTime);
						busStop.addRoute(busRoute);
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return busMap;
	}
}
